<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { getdirList, moveDoc } from '@/api/dashboard';
  import useLoading from '@/hooks/loading';
  import { collectAllKeys, deepTree } from '@/utils/utils';
  import usePageStore from '@/store/modules/page';
  import { deepClone } from '@arco-vue-pro-components/pro-components/es/pro-table/utils';
  import { Message } from '@arco-design/web-vue';
  const props = defineProps({
    visible: Boolean,
    params: Object,
  });
  const allExpandedKeys = ref([]);
  const expandedKeys = ref([]);
  const checkedKeys = ref([]);
  const treeData = ref([]); // 目录列表
  const pageStore = usePageStore();
  watch(
    () => props.params,
    (newVal) => {
      if (newVal?.id) {
        fetchData();
      }
    },
    {
      deep: true,
      immediate: true,
    }
  );

  const { loading, setLoading } = useLoading();
  defineOptions({
    name: 'publicfileedit',
  });

  async function fetchData() {
    const res = await getdirList({ dir_type: pageStore.currentDirType });
    treeData.value = deepTree(
      'dir_name',
      res.data,
      'dir_id',
      'dir_id',
      'Files'
    );
    allExpandedKeys.value = collectAllKeys(deepClone(treeData.value));
    expandedKeys.value = allExpandedKeys?.value;
  }

  const emit = defineEmits(['update:visible', 'refresh']);

  const handleSubmit = async () => {
    if (checkedKeys.value.length > 1) {
      Message.error('只能选择一个文件夹！');
      return false;
    }
    if (!(checkedKeys.value.length == 0)) {
      try {
        setLoading(true);
        const res = await moveDoc({
          doc_ids: props?.params.id, //要移动的目录id *
          target_dir_id: checkedKeys.value[0], //目标目录id，默认0 *
        });
        Message.success(res.msg);
        emit('update:visible', false);
        emit('refresh');
        setLoading(false);
      } catch (error) {
        setLoading(false);
      }
    } else {
      Message.error('请选择文件夹！');
    }
  };

  function toggleExpanded() {
    expandedKeys.value = expandedKeys?.value.length
      ? []
      : allExpandedKeys.value;
  }
</script>

<template>
  <a-modal
    title="移动文档"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <a-alert
      style="cursor: default; margin-bottom: 10px"
      @click="toggleExpanded"
    >
      <template #action>
        <icon-down />
      </template>
      <span>请选择需要移动到的文件夹</span>
    </a-alert>
    <a-tree
      :multiple="false"
      :checkable="true"
      v-model:checked-keys="checkedKeys"
      :check-strictly="true"
      v-model:expanded-keys="expandedKeys"
      :data="treeData"
    />
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
  .menu2 {
    height: 100%;
    box-sizing: border-box;
    padding: 10px;
    cursor: default !important;
    background: #f7f8fa;
  }
  :deep(.icon) {
    width: 1.2em !important;
    height: 1.2em !important;
  }

  :deep(.arco-tree-node-title-text) {
    font-size: 12px;
    width: 140px;
    overflow: hidden; /* 隐藏溢出内容 */
    white-space: nowrap; /* 禁止换行 */
    text-overflow: ellipsis; /* 显示省略号 */
  }

  :deep(.arco-tree-node-indent-block) {
    width: 0px !important;
  }

  :deep(.arco-tree-node-indent) {
  }
</style>
